﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FileData.Utils.Currency.FileUtils;
using FileData.Utils.Currency;
using FileData.DataUtils;
using FileData.Currency;
using FileData.DataUtils.ViewUtils;

namespace FileData.ViewManger
{
    public partial class RuleControl : UserControl
    {
        public RuleControl()
        {
            InitializeComponent();
            InitView();
        }

        private void InitView()
        {
       
        }

        string KeyFloder = "";
        string KeyFile = "";
        List<CurrentlyUtils.ShortDataInfo> DataTemp = new List<CurrentlyUtils.ShortDataInfo>();
        private void button1_Click(object sender, EventArgs e)
        {
            KeyFloder = new FloderHelper().SelectFloder();
            InitStart();
        }

        private void InitStart()
        {
            label2.Text = string.Format("当前已加载 {0} 条规则。", DataTemp.Count);
            if (KeyFloder == "")
                return;

            KeyFile = KeyFloder + "EnCoderHelper.xml";

            if (FileHelper.FileExits(KeyFile, true))

                DataTemp=   new CurrentlyUtils().ReadDataInfo(KeyFile);
       
             
            PutText();
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            PutText();
        }
        /// <summary>
        /// 自动添加规则
        /// </summary>
        private void PutText()
        {
            if (checkBox1.Checked)
                textBox2.Text = "{" + DataTemp.Count + "}";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AddRule();
        }
        /// <summary>
        /// 添加规则
        /// </summary>
        private void AddRule()
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageHelper.Alert("信息不能留空！");
                return;
            }
            DataTemp.Add(new CurrentlyUtils.ShortDataInfo { ReplaceForm=textBox1.Text,ReplaceTo =textBox2.Text});
            new CurrentlyUtils().WriteDataInfo(KeyFile, DataTemp);
            InitStart();
            textBox1.Text = "";
            textBox2.Text = "";

        }

        private void textBox2_DragDrop(object sender, DragEventArgs e)
        {
            string FilePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            richTextBox1.Text= new RuleTool().ReadFile(FilePath,textBox3.Text);
        }

        private void textBox2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link;
            else e.Effect = DragDropEffects.None;

        }
        int ClickCount = 0;
        private void label1_Click(object sender, EventArgs e)
        {
            ViewHelper();
        }

        private void ViewHelper()
        {
           
            if (ClickCount++ >3) {
                groupBox1.Visible = true;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new RuleTool().CreateFile(new FileHelper().SelectFile(), richTextBox1.Text,textBox3.Text,checkBox2.Checked);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            new RuleTool().ClearFile(new FileHelper().SelectFile() );
        }
    }
}
